﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Drawing;

public partial class Proccess_ProccessUploadImageContentProduct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UploadImageContentProductToServer();
    }

    private string EncodeString(string value)
    {
        System.Byte[] originalBytes;
        System.Byte[] encodedBytes;
        MD5 md5;
        md5 = new MD5CryptoServiceProvider();
        originalBytes = ASCIIEncoding.Default.GetBytes(value);
        encodedBytes = md5.ComputeHash(originalBytes);
        return System.BitConverter.ToString(encodedBytes);
    }
    public static string imagePa;
    public static string titImage;
    private void UploadImageContentProductToServer()
    {
        try
        {
            string action = Request["action"];
            if (action == "upload")
            {
                HttpPostedFile uploads = Request.Files["FileData"];
                string file = System.IO.Path.GetFileName(uploads.FileName);
                Random rand = new Random();
                int number = rand.Next(100000);
                string fileName = System.DateTime.Now.ToString() + number.ToString() + "" + System.DateTime.Now.Ticks;
                file = EncodeString(fileName + file) + ".jpg";
                string subfolder = string.Format("{0:MM-yyyy}", System.DateTime.Now);
                string folder = "~/ProductImages/" + subfolder;
                if (!Directory.Exists(folder))
                    Directory.CreateDirectory(Server.MapPath(folder));

                System.Drawing.Image bm = System.Drawing.Image.FromStream(uploads.InputStream);

                int width = (int)bm.Width;
                int height = (int)bm.Height;
                float ratio;
                float maxWidth = 650;
                float maxHeight = 800;

                //if (width > maxWidth)
                //{
                //    ratio = (float)width / (float)maxWidth;
                //    width = (int)(width / ratio);
                //    height = (int)(height / ratio);
                //}


                //if (height > maxHeight)
                //{
                //    ratio = (float)height / (float)maxHeight;
                //    height = (int)(height / ratio);
                //    width = (int)(width / ratio);
                //}





                //int width, height;
                float aspectRatio = (float)bm.Width / (float)bm.Height;

                if ((maxHeight > 0) && (maxWidth > 0))
                {
                    if ((bm.Width < maxWidth) && (bm.Height < maxHeight))
                    {


                    }
                    else if (aspectRatio > 1)
                    {

                        width = (int)maxWidth;
                        height = (int)(width / aspectRatio);
                        if (height > maxHeight)
                        {
                            height = (int)maxHeight;
                            width = (int)(height * aspectRatio);
                        }
                    }
                    else
                    {

                        height = (int)maxHeight;
                        width = (int)(height * aspectRatio);
                        if (width > maxWidth)
                        {
                            width = (int)maxWidth;
                            height = (int)(width / aspectRatio);
                        }
                    }
                }
                else if ((maxHeight == 0) && (bm.Width > maxWidth))
                {

                    width = (int)maxWidth;
                    height = (int)(width / aspectRatio);
                }
                else if ((maxWidth == 0) && (bm.Height > maxHeight))
                {

                    height = (int)maxHeight;
                    width = (int)(height * aspectRatio);
                }


                bm = ResizeBitmap((Bitmap)bm, width, height); /// new width, height
                bm.Save(Server.MapPath(folder + "/" + file));

                //uploads.SaveAs(Server.MapPath(folder + "/" + file));



                string imagePath = Utility.modHost + folder + "/" + file;
                string titleImage = folder + "/" + file;

                Session["imageProduct"] = Utility.modHost + "/" + "ProductImages/" + subfolder + "/" + file;
                Session["subFolderProduct"] = "ProductImages/" + subfolder + "/" + file;

                imagePa = Utility.modHost + "/" + "ProductImages/" + subfolder + "/" + file;
                titImage = "ProductImages/" + subfolder + "/" + file;
            }
            else if (action == "complete")
            {
                // string imagePa = (string)Session["imageFood"];
                //string titImage = (string)Session["subFolderFood"];
                //Response.Write("<img src=\"" + imagePa + "\" id=\"imgImageUpload\" alt=\"\" width=\"128px\" height=\"128px\" />");
                //Response.Write("<input type=\"hidden\" id=\"hdFoodImage\" name=\"hdFoodImage\" value=\"" + titImage + "\" />");
                //Response.Write("  <div id=\"regionFoodImage\"></div>");
                Response.Write(imagePa);
                Response.End();
            }


        }
        catch
        {

        }
    }

    private Bitmap ResizeBitmap(Bitmap b, int width, int height)
    {
        Bitmap result = new Bitmap(width, height);
        using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
            g.DrawImage(b, 0, 0, width, height);

        return result;
    }
}